package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.dao.BaseWorkorderMapper;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.model.controllerModel.WorkOrderCriteria;
import com.redoor.app.stake.workflow.model.serviceModel.BaseWorkOrderServiceBean;
import com.redoor.app.stake.workflow.model.serviceModel.WorkOrderNumBean;
import com.redoor.app.stake.workflow.service.BaseWorkOrderRedisService;
import com.redoor.app.stake.workflow.service.BaseWorkorderService;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-07 17:06:26.
 */
@Service
public class BaseWorkorderServiceImpl extends AbstractService<BaseWorkorder, String> implements BaseWorkorderService {

    @Autowired
    private BaseWorkorderMapper baseWorkorderMapper;

    @Autowired
    private BaseWorkOrderRedisService baseWorkOrderRedisService;

    @Autowired
    private BaseUserService baseUserService;

    @Override
    public int insert(BaseWorkorder workorder) {
        int res = 0;
        if (workorder!=null){
            Date today = new Date() ;
            /*写入工单编号*/
            workorder.setWorkorderno(this.genWorkOrdertNo());
            /*写入过期时间*/
            workorder.setExpireat(this.genWorkOrdErexipre(today));
            /*写入创建时间*/
            workorder.setCreatedat(today);
            /*写入更新时间*/
            workorder.setUpdatedat(today);
            /*写入工单状态 待维护*/
            workorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getCode()));

            res = baseWorkorderMapper.insert(workorder);
            
            //TODO 创建高频访问数据到缓存中
        }
        return res;
    }

    @Override
    public int update(BaseWorkorder baseWorkorder) {
        int result = 0 ;
        if (baseWorkorder != null) {
            Date today = new Date();
            /*写入审核时间*/
            baseWorkorder.setReviewrdate(today);
            /*写入更新时间*/
            baseWorkorder.setUpdatedat(today);
            /*写入跟新人uuid*/
            baseWorkorder.setUpdateruuid(baseUserService.getShiroUserUuid());
            result = baseWorkorderMapper.updateByPrimaryKeySelective(baseWorkorder);
        }
        return result;
    }

    public String genWorkOrdertNo(){
        return System.currentTimeMillis() + "" ;
    }

    @Override
    public Date genWorkOrdErexipre(Date today) {
        /*****begin 功能描述:修改过期时间为三十天之后  author:zhangMumu edit 2018/4/5 0005 0:28*****/
        return new Date(today.getTime() + 60 * 60 * 24 * 30);
        /*****end 功能描述:修改过期时间为三十天之后  author:zhangMumu edit 2018/4/5 0005 0:28*****/
    }

    @Override
    public List<BaseWorkorder> findMainApplyByTargetUuid(String targetUuid) {
        return baseWorkorderMapper.findMainApplyByTargetUuid(targetUuid);
    }

    @Override
    public List<BaseWorkorder> findMainChargeByTargetUuid(String targetUuid) {
        return baseWorkorderMapper.findMainChargeByTargetUuid(targetUuid);
    }

    @Override
    public List<BaseWorkOrderServiceBean> qryWorkOrder(WorkOrderCriteria baseWorkorderCriteria) {
        List<String> ouCodeList = baseUserService.districtPermission();
        baseWorkorderCriteria.setOuCodeList(ouCodeList);
        return baseWorkorderMapper.qryWorkOrder(baseWorkorderCriteria);
    }

    @Override
    public int qryWorkOrderNum(WorkOrderCriteria baseWorkorderCriteria) {
        List<String> ouCodeList = baseUserService.districtPermission();
        baseWorkorderCriteria.setOuCodeList(ouCodeList);
        return baseWorkorderMapper.qryWorkOrderNum(baseWorkorderCriteria);
    }

    @Override
    public WorkOrderNumBean countWorkOrderNum() {
        List<String> ouCodeList = baseUserService.districtPermission();
        WorkOrderCriteria workOrderCriteria = new WorkOrderCriteria();
        workOrderCriteria.setOuCodeList(ouCodeList);
        return baseWorkorderMapper.countWorkOrderNum(workOrderCriteria);
    }

}
